<?php
/**
 * 控制器基类
 *
 */
abstract class Controller extends Object 
{	
	protected $controller, $template, $defaultView;
	const NONE = 1;
	
    function __construct($controller)
    {
        $this->controller = $controller;
        
    }
    
	
    /**
	 * 是否GET方式请求而来的
	 *
	 * @return boolean
	 */
	public function isGetRequest()
	{
		return $_SERVER['REQUEST_METHOD'] == 'GET';
	}
	
	/**
	 * 是否POST方式请求而来的
	 *
	 * @return boolean
	 */
	public function isPostRequest()
	{
		return $_SERVER['REQUEST_METHOD'] == 'POST';
	}
	
	/**
	 * 是否HEAD方式请求而来的
	 *
	 * @return boolean
	 */
	function isHeadRequest()
	{
		return $_SERVER['REQUEST_METHOD'] == 'HEAD';
	}
	
	/**
	 * 是否PUT方式请求而来的
	 *
	 * @return boolean
	 */
	public function isPutRequest()
	{
		return $_SERVER['REQUEST_METHOD'] == 'PUT';
	}
	
	/**
	 * 是否DELETE方式请求而来的
	 *
	 * @return boolean
	 */
	public function isDeleteRequest()
	{
		return $_SERVER['REQUEST_METHOD'] == 'DELETE';
	}
	
	/**
	 * 是否AJAX方式请求而来的
	 *
	 * @return boolean
	 */
	public function isAjaxRequest()
	{
		return $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest';
	}
	
	/**
	 * 用session记录一个令牌token
	 *
	 */
	public function setToken()
	{
		$_SESSION['token'] = md5(microtime(true));
	}
	
	/**
	 * 验证一个令牌token
	 *
	 * @return boolean
	 */
	public function validToken()
	{
		$return = $_REQUEST['token'] === $_SESSION['token'] ? true : false;
		self::setToken();
		return $return;
	}
	
	/**
	 * 通过header跳转
	 *
	 * @param string $url
	 */
	protected function redirectByHeader($url)
	{
		header("location:$url");
	}

	
    /**
     * 提示信息，并halt
     *
     * @param string $message
     * @param string $url 
     * @param int $ms 延迟
     * @param boolean $success 成功与否
     */
    function showMessage($message, $url = null, $ms = 2000, $success = false)
	{
		if (is_array($message)) $message = implode('<br />', $message);
		$this->assign('message', $message);
		$this->assign('url', $url);
		$this->assign('ms', $ms);
		$this->assign('success', $success);
		$this->display('showMessage.html');
		exit;
	}
	
	public function dispatch()
	{
	
		ob_start();
		try {
			$return = call_user_func_array(array($this, $this->controller->action), $this->controller->args);
		} catch (Exception $e) {
			ob_end_clean();
			throw $e;
		}

		$text = ob_get_clean();
		switch (true) {
			case is_string($return):
				echo $return;
				break;
			case is_array($return):
				echo json_encode($return);
				break;
			case $this->hasDefaultViewScript():
				if(!isset($this->template)) {
					$this->template =  Factory::template();
				}
				$this->template->display($this->defaultView);
				break;
			default: 
				echo '';
				break;
		}

	}
	
 
	protected function hasDefaultViewScript() {
		$this->defaultView = $this->controller->controller.
		DIRECTORY_SEPARATOR.
		$this->controller->action.
		Config::get('template', 'ext');
		
		$view_file  = TEMPLATE_PATH.Config::get('template', 'name').DIRECTORY_SEPARATOR.$this->defaultView;

		return file_exists($view_file);
	}
	
	protected function getViewScript($name, $dir)
	{
		return $this->defaultView;
	}
	
	/*	实现自动调用template 输出 */
	public  function __call($method, $args_array) 
	{
		if(in_array($method, array('assign', 'display'))) {
			if(!isset($this->template)) {
				$this->template = Factory::template();
			}
			call_user_func_array(array($this->template, $method), $args_array);
		}
	}
	
}